﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package Engine.Classes {
    import flash.events.*;
    import Engine.Events.*;
    import Engine.Managers.*;
    import flash.display.*;
    import flash.geom.*;
    import Engine.Helpers.*;

	/**
	 *	支持旋转，还支持从DisplayObject初始化的Tile 
	 */
    public class TextureTile extends SimpleTile {

        protected var m_displayObject:DisplayObject = null;
        protected var m_thumbnail:DisplayObject;
        protected var m_thumbnailDirty:Boolean;

        override public function getThumbnailURL():String{
            return (m_imageFullPath);
        }
        override public function loadThumbnail():void{
            var _local1:Sprite;
            var _local2:Number;
            if ((((this.m_thumbnail == null)) || (this.m_thumbnailDirty))){
                if (m_rawBitmapData != null){
                    _local1 = new Sprite();
                    if (m_rawBitmapData.width > m_rawBitmapData.height){
                        _local2 = m_rawBitmapData.width;
                    } else {
                        _local2 = m_rawBitmapData.height;
                    };
                    _local1.graphics.beginBitmapFill(m_rawBitmapData);
                    _local1.graphics.drawRect(0, 0, _local2, _local2);
                    _local1.graphics.endFill();
                    this.m_thumbnail = _local1;
                    this.m_thumbnailDirty = false;
                };
            };
            dispatchEvent(new AssetEvent(this, AssetEvent.THUMBNAILCHANGED));
        }
        override public function getThumbnail():DisplayObject{
            return (this.m_thumbnail);
        }
        public function initializeFromDisplayObject(_arg1:DisplayObject):void{
            var _local2:BitmapData = new BitmapData(_arg1.width, _arg1.height);
            _local2.draw(_arg1, null, null, null, null, true);
            setBitmapData(_local2);
            this.generateTransformMatrix();
        }
        public function getDisplayObject():DisplayObject{
            if (this.m_displayObject == null){
                this.m_displayObject = LoadingManager.load(m_imageFullPath, this.onAssetImageLoaded, LoadingManager.PRIORITY_NORMAL, this.onAssetImageFault);
            };
            return (this.m_displayObject);
        }
        protected function onAssetImageLoaded(_arg1:Event):void{
            var _local2:DisplayObject = (_arg1.target.content as DisplayObject);
            if (_local2 != null){
                this.m_thumbnailDirty = true;
                m_rawBitmapData = new BitmapData(_local2.width, _local2.height, true, 0);
                m_rawBitmapData.draw(_local2);
                drawThumbnailBitmapData(_local2);
                this.generateTransformMatrix();
                dispatchEvent(new TileEvent(this, TileEvent.LOADED));
            };
            this.loadThumbnail();
        }
        protected function onAssetImageFault(_arg1:Event):void{
            dispatchEvent(new TileEvent(this, TileEvent.LOAD_FAILED));
        }
        override protected function generateTransformMatrix():void{
            var _local1:World;
            var _local2:IntVector2;
            var _local3:Point;
            if (m_rawBitmapData != null){
                _local1 = World.getInstance();
                _local2 = new IntVector2(tileWidth, tileHeight);
                _local3 = new Point((_local2.x / (m_rawBitmapData.width * Math.SQRT2)), (_local2.y / (m_rawBitmapData.height * Math.SQRT2)));
                m_transformMat = new Matrix();
                m_transformMat.translate((-(m_rawBitmapData.width) / 2), (-(m_rawBitmapData.height) / 2));
                m_transformMat.rotate((((45 + (_local1.getRotation() * 90)) * Math.PI) / 180));
                m_transformMat.translate(((m_rawBitmapData.width / 2) * Math.SQRT2), ((m_rawBitmapData.height / 2) * Math.SQRT2));
                m_transformMat.scale(_local3.x, _local3.y);
            };
        }

    }
}//package Engine.Classes 
